<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="./style.css" />
    <title>08_CSS 悬停效果之字形 D</title>
  </head>
  <body>
    <div id="switch-area">
      <input type="checkbox" id="switch" name="switch" />
      <label for="switch"></label>
    </div>
    <main>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Beatae voluptas amet ullam dignissimos a. Dicta
        ducimus porro saepe sit nemo, voluptates beatae harum nihil animi expedita ex placeat adipisci perspiciatis.
      </p>
    </main>
    <script>
      // 判断当前是否为暗色模式
      const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
      const btn = document.querySelector('#switch')
      if (isDarkMode) btn.checked = true
    </script>
    <script>
      const body = document.body
      btn.onclick = function () {
        if (btn.checked) {
          body.style.backgroundColor = '#121212'
        } else {
          body.style.backgroundColor = '#fff'
        }
      }
    </script>
  </body>
</html>
